package io.adrainty.bolee.insurance.handler;

import io.adrainty.bolee.framework.commons.utils.EnumUtil;
import io.adrainty.bolee.insurance.models.valobj.CheckRuleEnum;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/24 下午10:37
 */

@Component
public class InsuranceHandlerRegistry {

    private final Map<CheckRuleEnum, InsuranceHandler> handlerMap;

    public InsuranceHandlerRegistry(List<InsuranceHandler> handlerList) {
        this.handlerMap = new HashMap<>(handlerList.size());
        for (InsuranceHandler handler : handlerList) {
            this.handlerMap.put(handler.supportCheckRule(), handler);
        }
    }

    /**
     * 根据checkRule获取handler
     *
     * @param checkRule checkRule
     * @return handler
     */
    public InsuranceHandler getHandler(CheckRuleEnum checkRule) {
        return handlerMap.get(checkRule);
    }

    /**
     * 根据checkRule获取handler
     *
     * @param checkRule checkRule
     * @return handler
     */
    public InsuranceHandler getHandler(String checkRule) {
        CheckRuleEnum constants = EnumUtil.getEnumConstants(checkRule, CheckRuleEnum.class);
        return getHandler(constants);
    }


}
